package edu.guet.server.service.imple;

import edu.guet.server.mapper.StudentHomeworkMapper;
import edu.guet.server.model.dto.HomeworkDTO;
import edu.guet.server.model.vo.HomeworkDetailsVO;
import edu.guet.server.model.vo.HomeworkListVO;
import edu.guet.server.model.vo.SemHmkTestVO;
import edu.guet.server.model.vo.StuTestListVO;
import edu.guet.server.service.StudentHomeworkService;
import edu.guet.server.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@Slf4j
public class StudentHomeworkServiceImpl implements StudentHomeworkService {

    @Autowired
    private StudentHomeworkMapper homeworkMapper;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 获取作业列表
     * @return
     */
//    @Override
//    public List<HomeworkListVO> getHomeworkList(int user_id) {
//        List<HomeworkListVO> homeworkList = homeworkMapper.getHomeworkList(user_id);
////        List<Integer> homework_id = homeworkList.stream()
////                .map(HomeworkListVO::getId)
////                .collect(Collectors.toList());
//
//        return homeworkList;
//    }
    @Override
    public List<SemHmkTestVO> getHomeworkList(int semester_id, int user_id) {
        List<SemHmkTestVO> homeworkList = homeworkMapper.getHomeworkListBySemId(semester_id,user_id);
//        List<Integer> homework_id = homeworkList.stream()
//                .map(HomeworkListVO::getId)
//                .collect(Collectors.toList());

        return homeworkList;
    }

    /**
     * 通过id获取作业详情
     * @param id
     * @return
     */
    @Override
    public HomeworkDetailsVO getHomeworkDetails(int id, int user_id) {
        return homeworkMapper.getHomeworkById(id, user_id);
    }

    /**
     * 提交作业 如果提交过了则更新，否则插入
     * @param homeworkDTO
     */
    @Override
    public void submitHomework(HomeworkDTO homeworkDTO, MultipartFile file) {
        String url = null;
        //TODO 上传作业文件到云服务器
        if (file != null && !file.isEmpty()) {
            try {
                url = aliOSSUtils.upload(file);
//                url="https://www.icourse163.org/question/title/attachment.htm?key=696AFEA83049B5C4DAEF6A84270F7029-1713590653367&qid=3350433569";
                log.info("作业文件路径：" + url);
            } catch (Exception e) {

            }
        }
        homeworkDTO.setFile_url(url);
        if(homeworkMapper.isHomeworkSubmitted(homeworkDTO)){
            homeworkMapper.updateHomework(homeworkDTO);
        }else{
            homeworkMapper.insertHomework(homeworkDTO);
        }
    }

    @Override
    public HomeworkListVO getHmkStage(int userId, int hmkId) {
        return homeworkMapper.getHmkStage(userId,hmkId);
    }

    @Override
    public StuTestListVO getTestStage(int userId, int testId) {
        return homeworkMapper.getTestStage(userId,testId);
    }

}
